<?php

include_once 'Expression.php';
include_once 'CastType.php';

/**
 * Represents an expression cast to a data type.
 * 
 * @package UC_PHPCodeModel
 * @author Uros Calakovic
 * @example ../Examples/CastExpressionExample.php 
 */

class CastExpression extends Expression
{
    /**
     * CastExpression constructor
     * 
     * @param CastType $TargetType The destination type of the cast
     * @param Expression $Expression The expression to cast
     */
    
    public function __construct($TargetType, Expression $Expression = null)
    {
        parent::__construct();
        
        $this->set_TargetType($TargetType);
        
        $Expression == null ?
            $this->Expression = new Expression() :
            $this->set_Expression($Expression);
    }
    
    /**
     * Used for making private / protected variables 
     * accessible from outside the class.
     * 
     * @param string $name Name of the field to be accessed
     * @param mixed $value The value the field should be set to, usually a collection.
     * @see CodeObject::__set()
     */
    
    public function __set($name, $value)
    {
        parent::__set($name, $value);
        
        if($name == 'Expression')
        {
            $this->set_Expression($value);
        }
        
        if($name == 'TargetType')
        {
            $this->set_TargetType($value);
        }
    }
    
    /**
     * Used for making private / protected variables 
     * accessible from outside the class.
     * 
     * @param string $name Name of the field to be accessed
     * @return mixed The field that $name specified, usually a collection
     * @see CodeObject::__get()
     */
    
    public function __get($name)
    {
        $ret = parent::__get($name);
        
        if($ret != null)
        {
            return $ret;
        }
        
        if($name == 'Expression')
        {
            return $this->get_Expression();
        }
        
        if($name == 'TargetType')
        {
            return $this->get_TargetType();
        }
    }
    
    /**
     * The $TargetType getter method
     * @return CastType 
     */
    
    public function get_TargetType()
    {
        return $this->TargetType;
    }

    /**
     * The $TargetType setter method
     * @param CastType $TargetType One of the CastType constants
     * @throws InvalidArgumentException 
     * 
     * @assert (123) throws InvalidArgumentException
     * @assert ('name') throws InvalidArgumentException
     */
    
    public function set_TargetType($TargetType)
    {
        if(is_string($TargetType))
        {
            if(CastType::Defines($TargetType))
            {
                $this->TargetType = $TargetType;
            }
            else
            {
                throw new InvalidArgumentException(
                'The parameter must be a ' . 
                'CastType class constant.');
            }
        }
        else
        {
            throw new InvalidArgumentException(
                'The parameter must be a ' . 
                'CastType class constant.');
        }
    }

    /**
     * The $Expression getter method
     * @return Expression 
     */
    
    public function get_Expression()
    {
        return $this->Expression;
    }

    /**
     * The $Expression setter method
     * @param Expression $Expression 
     * 
     * @assert (123) throws PHPUnit_Framework_Error
     * @assert ('name') throws PHPUnit_Framework_Error
     */
    
    public function set_Expression(Expression $Expression)
    {
        $this->Expression = $Expression;
    }

    /**
     * The destination type of the cast
     * @var CastType One of the CastType constants 
     */
    
    private $TargetType;
    
    /**
     * The expression to cast
     * @var Expression Can be one of the Expression child classes 
     */
    
    private $Expression;
}

?>
